package com.bluesky.plum.uimodels.render.swing.components.table;

import java.util.List;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.bluesky.plum.uimodels.standard.components.Table;
import com.bluesky.plum.uimodels.standard.components.listbox.SelectionMode;

public class STable extends Table {

	private JScrollPane scrollPane;
	private JTable table;

	public STable() {
		super();

		// scrollPane = new JScrollPane();
		table = new JTable();
		// scrollPane.add(table);

		table.getSelectionModel().addListSelectionListener(
				new ListSelectionListener() {
					@Override
					public void valueChanged(ListSelectionEvent e) {
						if (getSelectChangeListener() != null)
							getSelectChangeListener().process();

					}
				});
	}

	@Override
	public void setObjects(List objects) {
		super.setObjects(objects);

		ObjectTableModel otm = new ObjectTableModel(data);

		table.setModel(otm);

	}
	
	@Override
	public void addObject(Object t) {
		
		super.addObject(t);
		setObjects(objects);
//		data = generateObjectArrayForObjects();
//		
//		ObjectTableModel otm=(ObjectTableModel)table.getModel();
//		otm.addRow(row)
	}

	@Override
	public Object getNativeComponent() {

		return table;
	}

	@Override
	public int getSelectedIndex() {

		return table.getSelectedRow();
	}

	@Override
	public int[] getSelectedIndices() {

		int[] rslt = table.getSelectedRows();

		return rslt;

	}

	@Override
	public void setSelectedIndex(int index) {
		table.getSelectionModel().setSelectionInterval(index, index);
	}

	@Override
	public void setSelectedIndices(int[] indices) {

		if (indices == null || indices.length == 0) {
			// do nothing
		} else
			table.getSelectionModel().setSelectionInterval(indices[0],
					indices[indices.length - 1]);
	}

	@Override
	public void setSelectionMode(SelectionMode selectionMode) {

		super.setSelectionMode(selectionMode);
		if (selectionMode == SelectionMode.Single)
			table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		else
			table
					.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
	}

}
